/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Artist;
import POJOS.MusicGroup;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import GUI.ListMusicGroupForm;

/**
 *
 * @author jim
 */
public class ListAsrtistForGroupForm extends javax.swing.JFrame {
    int selectedRow;
    MusicGroup musicGroup;
    Artist artist;

    /**
     * Creates new form NewJFrame
     */
    public ListAsrtistForGroupForm() {
        initComponents();
        checkButtons();
        //Επιλογή μίας γραμμής από τους διαθέσιμους καλλιτέχνες
        artistTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        //Αφαιρεί από τους διαθέσιμους καλλιτέχνες αυτούς που προσθέσαμε στο συγκρότημα (για να μην ξαναεμφανίζονται)
        for(Artist a: NewOrModifyMusicGroupForm.artistList){
            artistList.remove(a);
        }
    }
    
    //Για τροποποίηση συγκροτήματος
    public ListAsrtistForGroupForm(MusicGroup musicGroup) {
        initComponents();
        checkButtons();
        artistTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        
        RadioStationPUEntityManager.getTransaction().begin();
        musicGroup = RadioStationPUEntityManager.merge(musicGroup);
        RadioStationPUEntityManager.getTransaction().commit();
        
        //Αφαιρεί από τη λίστα με τους διαθέσιμους καλλιτέχνες αυτούς που ήδη υπάρχουν στο συγκρότημα
        for (Artist a : musicGroup.getArtistCollection()){
            artistList.remove(a);
        }
        //Αφαιρεί από τη λίστα με τους διαθέσιμους καλλιτέχνες αυτούς που προσθέσαμε στο συγκρότημα
        for(Artist a: NewOrModifyMusicGroupForm.artistList){
            artistList.remove(a);
        }
        //Προσθέτει στη λίστα με τους διαθέσιμους καλλιτέχνες αυτούς που διαγράψαμε από το συγκρότημα και δεν υπάρχουν στη λίστα
        for(Artist a:  musicGroup.getArtistCollection()){
            if(!NewOrModifyMusicGroupForm.artistList.contains(a)){
                artistList.add(a);
            }
        }
    }
    
    private void checkButtons(){
        selectedRow= artistTable3.getSelectedRow();
        add.setEnabled(selectedRow>=0);
        
        if (selectedRow>=0){
            artist= artistList.get(artistTable3.convertRowIndexToModel(selectedRow));
        }
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        artistQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT a FROM Artist a");
        artistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(artistQuery.getResultList());
        jScrollPane1 = new javax.swing.JScrollPane();
        artistTable3 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        cancel = new javax.swing.JButton();
        add = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΔΙΑΘΕΣΙΜΟΙ ΚΑΛΛΙΤΕΧΝΕΣ");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artistList, artistTable3);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastname}"));
        columnBinding.setColumnName("ΕΠΩΝΥΜΟ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstname}"));
        columnBinding.setColumnName("ΟΝΟΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artisticName}"));
        columnBinding.setColumnName("ΚΑΛΛΙΤΕΧΝΙΚΟ ΟΝΟΜΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        artistTable3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                artistTable3MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(artistTable3);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setText("Επιλέξτε καλλιτέχνη για προσθήκη στο συγκρότημα:");

        cancel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Actions-button-cancel-icon.png"))); // NOI18N
        cancel.setText("ΕΞΟΔΟΣ");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        add.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/1424870976_add.png"))); // NOI18N
        add.setText("ΕΙΣΑΓΩΓΗ");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(45, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(add)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancel)
                .addGap(90, 90, 90))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(add)
                    .addComponent(cancel))
                .addGap(22, 22, 22))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        // TODO add your handling code here:
        //Έλεγχος αν υπάρχει ο καλλιτέχνης στο συγκρότημα
        if (!NewOrModifyMusicGroupForm.artistList.contains(artist)) {
            //Προσθέτει στο συγκρότημα τον καλλιτέχνη
            NewOrModifyMusicGroupForm.artistList.add(NewOrModifyMusicGroupForm.artistList.size(), artist);
            //Αφαιρεί τον καλλιτέχνη από τους διαθέσιμους
            artistList.remove(artist);
        }else{
           JOptionPane.showMessageDialog(null, "Ο Καλλιτένης υπάρχει ήδη!", "ERROR", JOptionPane.ERROR_MESSAGE);
       }
     
       checkButtons();            
    }//GEN-LAST:event_addActionPerformed

    private void artistTable3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_artistTable3MouseClicked
        // TODO add your handling code here:
        checkButtons();
    }//GEN-LAST:event_artistTable3MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListAsrtistForGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListAsrtistForGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListAsrtistForGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListAsrtistForGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListAsrtistForGroupForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton add;
    public static java.util.List<POJOS.Artist> artistList;
    private javax.persistence.Query artistQuery;
    public static javax.swing.JTable artistTable3;
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
